package snmp

import (
	"fmt"
	"math"
	"strconv"
	"strings"

	"github.com/axgle/mahonia"
	g "github.com/gosnmp/gosnmp"
)

var enc = mahonia.NewDecoder("gbk")

func byte2string(b []byte) string {
	//判断假如乱码
	str := enc.ConvertString(string(b))
	if str == string(b) {
		return str
	}
	return enc.ConvertString(fmt.Sprintf("%x", b))
}

func filterSnmpValue(variables g.SnmpPDU) string {
	switch variables.Type {
	case g.OctetString:
		return byte2string(variables.Value.([]byte))
	case g.Integer, g.Counter32, g.Counter64, g.Gauge32, g.TimeTicks, g.IPAddress, g.ObjectIdentifier:
		return g.ToBigInt(variables.Value).String()
	default:
		return ""
	}
}

//过滤mac地址
func filterMac(mac string) string {
	//每两位插入字符
	var str strings.Builder
	for i := 0; i < len(mac); i += 2 {
		if str.String() != "" {
			str.WriteString(":")
		}
		str.WriteString(mac[i : i+2])
	}
	return str.String()
}

//内存使用率计算
func calcMemUsage(use, total int64) string {
	return strconv.FormatFloat(math.Round(float64(use)/float64(total)*100)/100, 'f', 2, 64)
}
